const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const lodash = require("lodash");


mongoConnected.then(async $ => {
  //计算distance_to-others
  let match_ids = (await mongoModels.TB21_luodian.aggregate([
    {
      "$project": {
        "match_id": "$match_id",
        "_id": 0
      }
    },
    {
      "$group": {
        "_id": null,
        "distinct": {
          "$addToSet": "$$ROOT"
        }
      }
    },
    {
      "$unwind": {
        "path": "$distinct",
        "preserveNullAndEmptyArrays": false
      }
    },
    {
      "$replaceRoot": {
        "newRoot": "$distinct"
      }
    }
  ])).map(e => e.match_id);
  let lajiju = []//落点数量和实际参加人数差异明显的 自定义模式
  for (let match_id of match_ids) {
    let matchEnd = await mongoModels.Event.findOne({
      match_id: match_id,
      _T: "LogMatchEnd"
    });
    let theMatch = await mongoModels.Match.findOne({
      match_id: match_id
    });
    let team_size = theMatch.team_size;
    let map_name = theMatch.map_name;
    let characters = matchEnd._doc.characters.map(e => e.character);
    let noAiCharacters = characters.filter(e => e.accountId.startsWith("account."));
    // debugger
    let evsParaLanding = await mongoModels.Event.find({
      match_id: match_id,
      _T: "LogParachuteLanding",
      "character.accountId": {
        "$in": noAiCharacters.map(e => e.accountId)
      }
    });
    if (evsParaLanding.length < noAiCharacters.length) {
      // debugger
      console.log(match_id);
      if (noAiCharacters.length - evsParaLanding.length > 1) {
        if (match_id.startsWith("match.bro.custom")) {
          lajiju.push(match_id);
        }
      }
      // console.log(evsParaLanding.map(e=>{
      //   return `${e._doc.character.name} ${e._doc.character.accountId}\n ${JSON.stringify(e._doc.character.location)}`
      // }))
    }

    for (let cha of noAiCharacters) {
      let landing_point = (evsParaLanding.find(e => e._doc.character.accountId === cha.accountId))
      if (!landing_point) {
        //说明他 没有跳伞 中途退了 不需要处理
        cha['landing'] = false;
      } else {
        landing_point = landing_point['_doc']['character']['location'];
        cha['landing'] = true;
        cha['dev'] = {
          landing_point
        }
      }
    }
    noAiCharacters = noAiCharacters.filter(e => e['landing']);
    // debugger
    for (let cha of noAiCharacters) {
      let teamId = cha.teamId;
      let others = noAiCharacters.filter(e => e.teamId != teamId);
      let distance = Infinity;
      for (let other of others) {
        let p1 = cha.dev.landing_point;
        let p2 = other.dev.landing_point;
        let d = Math.pow((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2, 0.5);
        // debugger
        if (d < distance) {
          distance = d;
        }
      }
      cha['dev']['distance_to_others'] = distance;
      let record = await mongoModels.TB21_luodian.findOne({
        match_id: match_id,
        account_id: cha.accountId
      });
      record.distance_to_others = distance;
      let saved = await record.save()
      // debugger
    }
    // debugger

  }
  debugger
})